/*
 * Copyright 2007-2010
 * This file is part of DP.
 *
 * DP is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * DP is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * DP; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package PD.data.parser.impl;

import PD.data.Row;
import PD.data.impl.peaklists.SimpleEdgeRow;
import PD.data.parser.Parser;
import com.csvreader.CsvReader;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author scsandra
 */
public class EdgesFilesParser implements Parser {

        private String datasetPath;
        private int rowsNumber;
        private int rowsReaded;
        List<Row> dataset;

        public EdgesFilesParser(String datasetPath) {
                if (datasetPath != null) {
                        this.rowsNumber = 0;
                        this.rowsReaded = 0;
                        this.datasetPath = datasetPath;
                        this.dataset = new ArrayList<Row>();
                }
        }

        public float getProgress() {
                return (float) rowsReaded / rowsNumber;
        }

        public void fillData() {
                try {
                        CsvReader reader = new CsvReader(new FileReader(datasetPath));
                        reader.readHeaders();
                        while (reader.readRecord()) {
                                getData(reader.getValues());
                                rowsReaded++;
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        private void getData(String[] sdata) {
                try {
                        Row lipid = new SimpleEdgeRow();
                        try {
                                lipid.setID(Integer.valueOf(sdata[0]));
                                lipid.setSourceVertexID(Integer.valueOf(sdata[1]));
                                lipid.setTargetVertexID(Integer.valueOf(sdata[2]));
                                lipid.setWeight(Double.valueOf(sdata[3]));
                                lipid.setDirection(sdata[4]);
                                lipid.setSourceDatabases(sdata[5]);
                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                        this.dataset.add(lipid);

                } catch (Exception exception) {
                        exception.printStackTrace();
                }
        }

        public List<Row> getList() {
                return this.dataset;
        }
}
